package com.mj.web;

import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

@RestController
public class HelloWorldController {
    /**
     * 1.返回单数数据Mono
     * 2.返回多个数据Flux
     */
    @GetMapping("/hello")
    public Mono<String> helloWorld() {
        return Mono.just("Hello World");
    }
    @GetMapping("/hi")
    public Flux<String> hellHi() {
        return Flux.just("Hi", "Hello","thanks");
    }

    /**
     *
     * chatgpt 都在使用
     */
    @GetMapping(value="/hee", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> hellHEE() {

        return Flux.range(1,10)
                .map(i -> "HEE" + i)
                .delayElements(Duration.ofMillis(100));
    }
    //文件上传
    @GetMapping("/mao")
    public Mono<String> helloMao(ServerWebExchange exchange, WebSession webSession,
                                 HttpMethod method, FilePart filePart) {
      ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        Object aaa=webSession.getAttribute("aaa");
        String name=method.name();
        //filePart.transferTo(filePart.filename())//零拷贝
        return Mono.just("Hello World");
    }
}
